package xyz.yuzegod.megawalls.classes.normal.squid;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.PlayerUtils;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MainSkill extends Skill
{
    private static final Set<Material> set;
    
    public MainSkill(final Classes classes) {
        super("鱿鱼飞溅", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 5;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 0.3667;
            }
            case 2: {
                return 0.45;
            }
            case 3: {
                return 0.5333;
            }
            case 4: {
                return 0.6167;
            }
            case 5: {
                return 0.7;
            }
            default: {
                return 0.3667;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7将敌人传送至你的位置并且");
            lore.add("   §7对3格以内的所有敌人造成3点伤害。");
            lore.add("   §7你会恢复你造成的伤害的§a" + StringUtils.percent(this.getAttribute(level)) + "§7。");
            lore.add("   §7最多恢复7点生命。");
            lore.add(" ");
            lore.add("§7冷却时间:§a2秒");
            return lore;
        }
        lore.add(" §8▪ §7将敌人传送至你的位置并且");
        lore.add("   §7对3格以内的所有敌人造成3点伤害。");
        lore.add("   §7你会恢复你造成的伤害的§8" + StringUtils.percent(this.getAttribute(level - 1)) + " ➜");
        lore.add("   §a" + StringUtils.percent(this.getAttribute(level)) + "§7。");
        lore.add("   §7最多恢复7点生命。");
        lore.add(" ");
        lore.add("§7冷却时间:§a2秒");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkillLevel();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkillLevel();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        final Player player = gamePlayer.getPlayer();
        if (Squid.skillCooldown.getOrDefault(gamePlayer, 0) > 0) {
            player.sendMessage("§c技能正在冷却中！");
            return false;
        }
        Player target = null;
        for (final Block block : player.getLineOfSight(MainSkill.set, 4)) {
            for (final Player player2 : this.getNearbyPlayers(block.getLocation(), player, 2)) {
                if (target == null || player2.getLocation().distance(player.getLocation()) < target.getLocation().distance(player.getLocation())) {
                    target = player2;
                }
            }
        }
        if (target == null) {
            player.sendMessage("§c没有玩家在目标范围内！");
            return false;
        }
        target.teleport(player.getLocation());
        target.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 60, 0));
        final int level = this.getPlayerLevel(gamePlayer);
        double heal = 0.0;
        for (final Player player3 : this.getNearbyPlayers(player, 3)) {
            player3.damage(3.0, (Entity)player);
            heal = ((heal + 3.0 * this.getAttribute(level) > 7.0) ? 7.0 : (heal + 3.0 * this.getAttribute(level)));
        }
        PlayerUtils.heal(player, heal);
        Squid.skillCooldown.put(gamePlayer, 2);
        return true;
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        return this.getClasses().getNameColor() + "§l" + this.getName() + " " + ((Squid.skillCooldown.getOrDefault(gamePlayer, 0) == 0) ? ((gamePlayer.getEnergy() == 100) ? "§a§l✓" : "§c§l✕") : ("§c§l" + Squid.skillCooldown.get(gamePlayer) + "秒"));
    }
    
    private List<Player> getNearbyPlayers(final Player player, final int radius) {
        final List<Player> players = new ArrayList<Player>();
        final GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
        for (final Player other : PlayerUtils.getNearbyPlayers((Entity)player, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gameOther.isSpectator() && !gamePlayer.getGameTeam().isInTeam(gameOther)) {
                if (other.getLocation().distance(player.getLocation()) > radius) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
    
    private List<Player> getNearbyPlayers(final Location location, final Player player, final int radius) {
        final List<Player> players = new ArrayList<Player>();
        final GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
        for (final Player other : PlayerUtils.getNearbyPlayers(location, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gameOther.isSpectator() && !gamePlayer.getGameTeam().isInTeam(gameOther)) {
                if (other.getLocation().distance(location) > radius) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
    
    static {
        set = new HashSet<Material>();
        for (final Material material : Material.values()) {
            MainSkill.set.add(material);
        }
    }
}
